package com.flyou.ccnucenter.ui.activity.login;


import android.app.ProgressDialog;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.MyUser;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.ui.activity.MainActivity;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.Md5Parser;
import com.flyou.ccnucenter.utils.RegexPatternUtils;
import com.flyou.ccnucenter.widget.ClearEditText;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class SetNewPasswordActivity extends BaseActivity1 implements TextWatcher, View.OnClickListener {
    private String phone;
    private String Md5password;
    private ClearEditText mSetNewPasswordEtPassword;
    private ClearEditText mSetNewPasswordEtRepassword;
    private Button mSetNewPasswordBtSubmit;

    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_set_new_password);

        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);

        mSetNewPasswordEtPassword = (ClearEditText) findViewById(R.id.setNewPassword_et_password);
        mSetNewPasswordEtRepassword = (ClearEditText) findViewById(R.id.setNewPassword_et_Repassword);
        mSetNewPasswordBtSubmit = (Button) findViewById(R.id.setNewPassword_bt_submit);
    }

    @Override
    protected void initDate() {
        phone = getIntent().getStringExtra("phone");

        mTitlebarTvTitle.setText("设置新密码");

    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        mSetNewPasswordBtSubmit.setOnClickListener(this);
        mSetNewPasswordEtRepassword.addTextChangedListener(this);

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() > 0) {
            mSetNewPasswordBtSubmit.setEnabled(true);
        } else {
            mSetNewPasswordBtSubmit.setEnabled(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.titlebar_rl_back:
                finish();
                break;
            case R.id.setNewPassword_bt_submit:

                if (mSetNewPasswordEtPassword.getText().toString().trim().isEmpty()
                        || mSetNewPasswordEtRepassword.getText().toString().trim().isEmpty()) {
                    Toast.makeText(this, R.string.regex_password_empty, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mSetNewPasswordEtPassword.getText().toString().trim().
                        equals(mSetNewPasswordEtRepassword.getText().toString().trim())) {
                    Toast.makeText(this, R.string.regex_password_equals, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!RegexPatternUtils.isPassword(mSetNewPasswordEtPassword.getText().toString().trim())) {
                    Toast.makeText(this, R.string.regex_password_error, Toast.LENGTH_SHORT).show();
                    return;
                }
                Md5password = Md5Parser.md5Parser(mSetNewPasswordEtRepassword.getText().toString().trim());
                setPassWord(phone, Md5password);

                break;
        }

    }

    private void setPassWord(String phone, final String password) {
        BmobQuery<MyUser> query = new BmobQuery<MyUser>();
        query.addWhereEqualTo("mobilePhoneNumber", phone);
        query.findObjects(SetNewPasswordActivity.this, new FindListener<MyUser>() {
            @Override
            public void onStart() {
                mProgressDialog = ProgressDialog.show(SetNewPasswordActivity.this, null, "正在提交数据", false, false);
                super.onStart();
            }

            @Override
            public void onFinish() {
                mProgressDialog.dismiss();
                super.onFinish();
            }

            @Override
            public void onSuccess(List<MyUser> list) {
                MyUser myUser1 = new MyUser();
                myUser1.setPassword(password);
                Toast.makeText(SetNewPasswordActivity.this, list.get(0).getObjectId(), Toast.LENGTH_SHORT).show();
                myUser1.update(SetNewPasswordActivity.this, list.get(0).getObjectId(), new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(SetNewPasswordActivity.this, "密码修改成功,请重新登录", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(SetNewPasswordActivity.this, MainActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onFailure(int i, String s) {

                        String message = BmobErrorCodeUtils.getMessage(i, s);
                        Toast.makeText(SetNewPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                });

            }

            @Override
            public void onError(int i, String s) {
                String message = BmobErrorCodeUtils.getMessage(i, s);
                Toast.makeText(SetNewPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
